package com.hrms.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hrms.entity.User;
import com.hrms.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    private UserMapper userMapper;

    public List<Map<String, Object>> selectAll() {
        // 连接表user和表customer，查询所有记录
        return userMapper.selectAll();
    }

    public List<Map<String, Object>> selectCustomer(User user) {
        // 连接表user和表customer，根据user_id查询表customer中的所有记录
        return userMapper.selectCustomer(user.getUser_id());
    }

    public boolean passwordisRight(User user) {
        // 根据user_name和user_password查询账号密码是否匹配
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("user_name", user.getUser_name())
                .eq("user_password", user.getUser_password());
        int count = (int) count(wrapper);

        if(count == 0) return false;
        else return true;
    }

    public boolean accountExist(User user) {
        // 查询账号是否存在
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", user.getUser_id());
        int count = (int) count(wrapper);

        if(count == 0) return false;
        else return true;
    }

    public boolean nameExist(User user) {
        // 查询账号名是否存在
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("user_name", user.getUser_name());
        int count = (int) count(wrapper);

        if(count == 0) return false;
        else return true;
    }

    public boolean checkStatue(User user) {
        // 根据user_id和user_status查询账号状态是否匹配
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", user.getUser_id())
                .eq("user_status", user.getUser_status());
        int count = (int) count(wrapper);

        if(count == 0) return false;
        else return true;
    }

    public boolean checkStatuebyName(User user) {
        // 根据user_name和user_status查询账号名状态是否匹配
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("user_name", user.getUser_name())
                .eq("user_status", user.getUser_status());
        int count = (int) count(wrapper);

        if(count == 0) return false;
        else return true;
    }

    public boolean updateStatue(User user) {
        // 根据user_id修改user_status
        // true表示修改成功
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", user.getUser_id());

        return update(user, wrapper);
    }

    public boolean updateStatuebyName(User user) {
        // 根据user_name修改user_status
        // true表示修改成功
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("user_name", user.getUser_name());

        return update(user, wrapper);
    }

    public boolean addUser(User user) {
        // 在表user中添加一条记录
        // true表示添加成功
        return save(user);
    }

    public boolean addCustomer(User user) {
        // 根据user_id修改customer_id
        // true表示修改成功
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", user.getUser_id());

        return update(user, wrapper);
    }

    public String selectCustomerId(User user) {
        // 根据user_id返回相应的customer_id
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", user.getUser_id())
                .select("customer_id");

        List<Map<String, Object>> list = listMaps(wrapper);

        return list.get(0).get("customer_id").toString();
    }

    public boolean updatebyAdmin(User user) {
        // 根据user_id修改user_name和user_password
        // true表示修改成功
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", user.getUser_id());

        return update(user, wrapper);
    }

    public String selectUserId(User user) {
        // 根据user_name返回相应的user_id
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("user_name", user.getUser_name())
                .select("user_id");

        List<Map<String, Object>> list = listMaps(wrapper);

        return list.get(0).get("user_id").toString();
    }
}
